import { fileURLToPath, URL } from 'node:url';

import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import qiankun from 'vite-plugin-qiankun';
import { name } from './package.json';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import { resolve } from 'path';
const port = 4000;
// https://vitejs.dev/config/
const env = loadEnv(process.env.NODE_ENV!, './');
const publicPath =
  process.env.NODE_ENV === 'production'
    ? `${env.VITE_APP_BASEURL}/${name}/`
    : `http://localhost:${port}/${name}/`;
export default defineConfig({
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  base: publicPath,
  // publicDir: publicPath,
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: {
          hack: `true; @import (reference) "${resolve('./src/styles/index.less')}";`,
          // ...envTheme,
        },
        // additionalData: `@import "./src/styles/index.less";`,
        javascriptEnabled: true,
      },
    },
  },
  plugins: [
    vue({ reactivityTransform: true }),
    vueJsx(),
    qiankun(name, { useDevMode: true }),
    Components({
      resolvers: [AntDesignVueResolver({ importStyle: false })],
      dts: fileURLToPath(new URL('./src/types/auto-import-components.d.ts', import.meta.url)),
    }),
    AutoImport({
      imports: ['vue', 'vue-router'],
      dts: fileURLToPath(new URL('./src/types/auto-import.d.ts', import.meta.url)),
    }),
  ],
  server: {
    port,
  },
});
